/**
 * 存储用户信息
 */
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { Md5 } from 'ts-md5';

@Injectable({
  providedIn: 'root'
})

export class UserService {
  public usertoken: string;//token
  public roomid: string;//roomid
  public versionCode: number;

  public os: number = 3;//3代表web
  public imei: string = "";//生成的唯一码
  public ncode: any;//seed + imei + os + reqtime 
  public ispc:boolean=false;//是否是pc
  public deployUrl:string;//图片地址

  constructor() { }
  //获取加密的MD5，进行两次md5加密
  public getMd5Ncode(reqtime: any) {
    this.ncode = Md5.hashStr((environment.seed + "" + this.imei + "" + this.os + "" + reqtime), false);
    this.ncode = Md5.hashStr(this.ncode.toUpperCase(), false);
    return this.ncode.toUpperCase();
  }

  //图片地址
  public getDeployUrl(){
    return environment.deployUrl;
  } 
  public setUserToken(usertoken: string): void {
    this.usertoken = usertoken;
  }
  public getUserToken(): string {
    return this.usertoken;
  }
  public setRoomid(roomid: string): void {
    this.roomid = roomid;
  }
  public getRoomid(): string {
    return this.roomid;
  }
  public setVersionCode(versionCode: number) {
    this.versionCode = versionCode;
  }
  public getVersionCode() {
    return this.versionCode;
  }
  public setImei(imei: any) {
    this.imei = imei;
  }
  public getImei() {
    return this.imei;
  }
  /**
   * 判断是不是电脑直播
   * @param ispc 
   */
  public getIsPc(){
    var userAgentInfo = navigator.userAgent;
        var Agents = [
            "Android",
            "iPhone",
            "SymbianOS",
            "Windows Phone",
            "iPad",
            "iPod"
        ];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
  }
  public showToast(msg: string, duration: number) {
    duration = isNaN(duration) ? 2000 : duration;
    var m = document.createElement('div');
    m.innerHTML = msg;
    m.style.cssText = "width:60%; min-width:180px;font-size:14px; background:#000; opacity:0.6; height:auto;min-height: 30px; color:#fff; line-height:30px; text-align:center; border-radius:4px; position:fixed; top:60%; left:20%; z-index:999999;";
    document.body.appendChild(m);
    setTimeout(function () {
      var d = 0.5;
      m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
      m.style.opacity = '0';
      setTimeout(function () { document.body.removeChild(m) }, d * 1000);
    }, duration);
  }

}
